﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;

namespace MplusM.MovieDatabaseWorkers
{
    public interface IMovieDatabaseWorker
    {
        event WorkerContentHandler ContentLoaded;
        event WorkerErrorHandler ContentLoadedFailed;
        event WorkerImageHandler CoverLoaded;
        event WorkerErrorHandler CoverLoadedFailed;
        String Url { get; set; }
        Encoding Encoding { get; set; }
        void LoadContentAsync();
        void LoadCoverAsync();
    }

    public delegate void WorkerErrorHandler(Exception ex);
    public delegate void WorkerContentHandler(String content);
    public delegate void WorkerImageHandler(Image image);

	/// <summary>
	/// class representing parsed details of a media
	/// </summary>
    public class MovingPicture
    {
        public String OriginalName { get; set; }
        public String TranslatedName { get; set; }
        public String Country { get; set; }
        public String Director { get; set; }
        public String Content { get; set; }
        public DateTime ReleaseDate { get; set; }
        public double Rating { get; set; }
        public ushort Length { get; set; }
        public List<String> Genres { get; set; }
        public List<String> Cast { get; set; }
    }
	/// <summary>
	/// class encapsulates all the methods used to process retrieved images
	/// </summary>
    public static class ImageProcessing
    {
		/// <summary>
		/// resizes an image to fit the cover imageBox in the main form
		/// </summary>
		/// <param name="img">an image to be resised </param>
		/// <param name="width">the new width of a given picture to which it should be converted</param>
		/// <returns>an image with proportions <value>width</value> and <value>height</value> in changed proportions</returns>
        public static Image ResizeImageToWidth(this Image img, int width)
        {
            if (img.Width <= width)
            {
                return img;
            }
            double ratio = (double)width / img.Width;
            Bitmap bmp = new Bitmap(img, new Size(width, (int)(img.Height * ratio)));
            return (Image)bmp;
        }

		/// <summary>
		/// transforms image into a MimeType
		/// </summary>
		/// <param name="img">Image to be transformed</param>
		/// <returns>string of a mimeType if found; string "image/unknown" if no hit </returns>
        public static String MimeType(this Image img)
        {
            foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())
            {
                if (codec.FormatID == img.RawFormat.Guid)
                    return codec.MimeType;
            }
            return "image/unknown";
        }
    }
}
